package com.twxm.breed.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.twxm.breed.service.DissolvedOxygenService;
import com.twxm.breed.service.WaterFactorService;
import com.twxm.breed.service.WeatherFactorService;
import com.twxm.breed.util.List2JSONArray;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/data")
@RestController
public class CollectionController {
    @Autowired
    private WaterFactorService waterFactorService;
    @Autowired
    private WeatherFactorService weatherFactorService;
    @Autowired
    private DissolvedOxygenService dissolvedOxygenService;

    @PostMapping("/get_data")
    public JSONArray getFactor(@RequestParam("pondid") String pondId,
                               @RequestParam("factor") String factor){
        switch (factor) {
            case "water":
                return List2JSONArray.waterList2JsonArray(waterFactorService.getByPondId(pondId),pondId,factor);
            case "weather":
                return List2JSONArray.weatherList2JsonArray(weatherFactorService.getByPondId(pondId),pondId,factor);
            case "oxygen":
                return List2JSONArray.oxygenList2JsonArray(dissolvedOxygenService.getByPondId(pondId),pondId,factor);
            default:
                return new JSONArray();
        }
    }


    @PostMapping("/get_element")
    public JSONObject getElementData(@RequestParam("el") String el){
        List<String> pondIds;
        switch (el){
            case "oxygenContent":
                pondIds = dissolvedOxygenService.getPondId();
                List oxygenList = dissolvedOxygenService.getOxygenInfo(el);
                return List2JSONArray.elList2Json(oxygenList,pondIds, el);
            case "waterTemperature":
            case "PH":
            case "ammoniaNitrogen":
            case "totalNitrogen":
            case "totalPhosphorus":
            case "permanganate":
            case "nitrite":
                pondIds = waterFactorService.getPondId();
                List waterList = waterFactorService.getWaterInfo(el);
                return List2JSONArray.elList2Json(waterList,pondIds, el);
            case "temperature":
            case "windSpeed":
            case "airPressure":
            case "illumination":
            case "humidity":
                pondIds = weatherFactorService.getPondId();
                List weatherList =  weatherFactorService.getWeatherInfo(el);
                return List2JSONArray.elList2Json(weatherList,pondIds,el);
            default:
                return new JSONObject();
        }
    }

//    @SneakyThrows
    @PostMapping("/get_oldData")
    public JSONArray getOldFactor(@RequestParam("pondid") String pondId,
                                  @RequestParam("factor") String factor,
                                  @RequestParam("time")String time){
        ObjectMapper objectMapper = new ObjectMapper();
        Date date = null;
        try {
            date = objectMapper.readValue(time, Date.class);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("pondId",pondId);
        map.put("date",date);
        switch (factor) {
            case "water":
                return List2JSONArray.waterList2JsonArray(waterFactorService.getByPondIdAndDate(map),pondId,factor);
            case "weather":
                return List2JSONArray.weatherList2JsonArray(weatherFactorService.getByPondIdAndDate(map),pondId,factor);
            case "oxygen":
                return List2JSONArray.oxygenList2JsonArray(dissolvedOxygenService.getByPondIdAndDate(map),pondId,factor);
            default:
                return new JSONArray();
        }
    }

    @PostMapping("/get_oldElement")
    public JSONObject getOldElementData(@RequestParam("el") String el,
                                        @RequestParam("time") String time){
        ObjectMapper objectMapper = new ObjectMapper();
        Date date = null;
        try {
            date = objectMapper.readValue(time, Date.class);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        List<String> pondIds;
        switch (el){
            case "oxygenContent":
                pondIds = dissolvedOxygenService.getPondId();
                List oxygenList = dissolvedOxygenService.getOxygenInfoByDate(date);
                return List2JSONArray.elList2Json(oxygenList,pondIds, el);
            case "waterTemperature":
            case "PH":
            case "ammoniaNitrogen":
            case "totalNitrogen":
            case "totalPhosphorus":
            case "permanganate":
            case "nitrite":
                pondIds = waterFactorService.getPondId();
                List waterList = waterFactorService.getWaterInfoByDate(date);
                return List2JSONArray.elList2Json(waterList,pondIds, el);
            case "temperature":
            case "windSpeed":
            case "airPressure":
            case "illumination":
            case "humidity":
                pondIds = weatherFactorService.getPondId();
                List weatherList =  weatherFactorService.getWeatherInfoByDate(date);
                return List2JSONArray.elList2Json(weatherList,pondIds,el);
            default:
                return new JSONObject();
        }
    }
}

